/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ants;
//import com.sun.xml.internal.ws.api.pipe.NextAction;
import java.io.*;
import java.awt.Point;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
 *
 * @author uhgy3k
 */
class Mapa {
    final int wallfix = 2;
    final int offset  = 1;
    int w;
    int h;
    int nplayers = 0;
    int food = 0;
    int[] maxAntsT;
    Hashtable colony;
    Celda[][] cells;
    FileInputStream file;
    Scanner reader;
    
    public Mapa(String path)throws IOException{
        StringTokenizer tok;
        String input;
        String tmp;
        maxAntsT = new int[3];
        colony = new Hashtable();
        file = new FileInputStream(path);
        reader = new Scanner(file, "UTF-8");
        //definimos el numero maximo de tipos de hormigas
        input = reader.nextLine();
        tok = new StringTokenizer(input, ":");
        for(int i = 0; i < maxAntsT.length; i++){
            maxAntsT[i] = Integer.parseInt(tok.nextToken());
        }
        //definimos el tama�o del mapa
        input = reader.nextLine();
        tok = new StringTokenizer(input, ":");
        w = Integer.parseInt(tok.nextToken());
        h = Integer.parseInt(tok.nextToken());
        //creamos la matriz de celdas
        cells = new Celda[h+wallfix][w+wallfix];
        //rellenamos los bordes inferior y superior
        for(int i = 0; i < (w+wallfix); i++){
            cells[0][i] = new Celda();
            cells[h+offset][i] = new Celda();
            cells[0][i].comida = 0;
            cells[h+offset][i].comida = 0;
            cells[0][i].tipo = Constantes.obstaculo;
            cells[h+offset][i].tipo = Constantes.obstaculo;
        }
        //rellenamos los bordes laterales
        for(int i = 0; i < (h+wallfix); i++){
            cells[i][0] = new Celda();
            cells[i][w+offset] = new Celda();
            cells[i][0].comida = 0;
            cells[i][w+offset].comida = 0;
            cells[i][0].tipo = Constantes.obstaculo;
            cells[i][w+offset].tipo = Constantes.obstaculo;
        }
        //rellenamos el mapa
        for(int i = offset; i < (h+offset) && reader.hasNext(); i++){
            input = reader.nextLine();
            
            tok = new StringTokenizer(input, " ");
            for(int j = offset; j < (w+offset); j++){
                tmp = tok.nextToken();
                System.out.print(tmp);
                cells[i][j] = new Celda();
                if(tmp.equals("0")){
                    cells[i][j].tipo = Constantes.libre;
                }
                else if(tmp.equals("w")){
                    cells[i][j].tipo = Constantes.obstaculo;
                }
                else if(isPlayer(tmp)){
                    cells[i][j].tipo = Constantes.libre;
                    nplayers++;
                    colony.put(new Integer(nplayers), new Point(i,j));
                }
                else if(isFood(tmp)){
                    cells[i][j].tipo = Constantes.libre;
                    cells[i][j].comida = Integer.parseInt(tmp.substring(offset));
                }
            }
            System.out.println();
        }
        /*
        for(int x = 0; x < cells.length; x++) {
            for(int y = 0; y < cells[x].length; y++)
                System.out.print(cells[x][y].tipo);
            System.out.println();
        }
        */
        reader.close();
        file.close();
    }
    
    private boolean isPlayer(String input){
        if(input.charAt(0) == 'u')
            return true;
        return false;
    }
    private boolean isFood(String input){
        if(input.charAt(0) == 'c'){
            food += Integer.parseInt(input.substring(1));
            return true;
        }
        return false;
    }
    public Celda getCelda(int x, int y){
        return cells[y][x];
    }
    public void setCelda(Celda cell, int x, int y){
        cells[y][x] = cell;
    }
    public Point getHormiguero(int player){
        Point loc = (Point)colony.get(new Integer(player));
        return loc;
    }
    public Celda[][] getMap(){
        return cells;
    }
    
    public int maxGuerreras(){
        return maxAntsT[0];
    }
    public int maxExploradoras(){
        return maxAntsT[1];
    }
    public int maxObreras(){
        return maxAntsT[2];
    }
    public int maxJugadores(){
        return nplayers;
    }
    public int getTotalFood(){
        return food;
    }
}